#include "log.h"
#include "tool.h"
#include "usart.h"
#include "user_shell.h"
#include "file_operate.h"

void log_write(char *buff,short len);

Shell shell;

char shellBuffer[512];
Log uartLog = {
    .write = log_write,
    .active = LOG_ENABLE,
    .level = LOG_DEBUG
};
    
signed short shell_read(char *buff, unsigned short len) 
{
	uint8_t ucGetChar;
	if(HAL_UART_Receive(&huart1, &ucGetChar, 1, 0x00) == HAL_OK)
	{
		buff[0] = ucGetChar;
		return 1;
	}
	
	
	return 0;

}

signed short shell_write(char *buff, unsigned short len)
{
	HAL_UART_Transmit(&huart1, (uint8_t *)buff, len, 0xFF);
  return len;
}

void log_write(char *buff,short len)
{
	HAL_UART_Transmit(&huart1, (uint8_t *)buff, len, 0x00);
}

void userShellInit(void)
{
    shell.write = shell_write;
    shell.read = shell_read;
    shellInit(&shell, shellBuffer, 512);
    logRegister(&uartLog, &shell);
}

void CreateTestBug(void)
{
	void EnableDiv0Bug(void);
	void A(int n, int m);
	
	EnableDiv0Bug();
	A(100, 0);

}

void CoreDumpDel(void)
{
	FatFs_DeleteFile("coredump.txt");
}


void CoreDumpRead(void)
{
	FatFs_ReadTXTFile("coredump.txt");
}

SHELL_EXPORT_CMD(
SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_DISABLE_RETURN,
CoreDumpRead, CoreDumpRead, Core Dump Read);


SHELL_EXPORT_CMD(
SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_DISABLE_RETURN,
CoreDumpDel, CoreDumpDel, Core Dump Del);


SHELL_EXPORT_CMD(
SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_DISABLE_RETURN,
CreateBug, CreateTestBug, Create Test Bug);


SHELL_EXPORT_CMD(
SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_DISABLE_RETURN,
FatFsGetDiskInfo, FatFs_GetDiskInfo, FatFs GetDiskInfo);

SHELL_EXPORT_CMD(
SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_DISABLE_RETURN,
MountMkfs, Mount_Mkfs, Mount Mkfs);
